//
//  ABHomeViewController.swift
//  hEhL
//
//  Created by 本 on 16/5/5.
//  Copyright © 2016年 本. All rights reserved.
//

import UIKit

class ABHomeViewController: UIViewController {
    var scrollView = UIScrollView()
    var titlesView = UIView()
    var lastBtn = ABTitleViewButton()
    var lineView = UIView()
    var oneBool = Bool()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildView()
        setupScrollView()
        setupTitlesView()
        let i = scrollView.contentOffset.x / self.scrollView.frame.size.width
        let btn   = self.titlesView.subviews[Int(i)] as! ABTitleViewButton
        titleButtonClick(btn)


        
    }
    
    
}
extension ABHomeViewController {
    func setupChildView(){
        addChildViewController(topLineViewController.initWithNib())
        addChildViewController(videoViewController())
        addChildViewController(competeViewController())
        addChildViewController(belleViewController())
        addChildViewController(picViewController())
        addChildViewController(wallPaperViewController())

    
    }
    func setupScrollView (){
        let count = self.childViewControllers.count
        automaticallyAdjustsScrollViewInsets = false
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.scrollsToTop = false
        scrollView.frame = self.view.bounds;
        scrollView.backgroundColor = UIColor.redColor()
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.showsVerticalScrollIndicator = false;
        let w  = scrollView.frame.size.width
        scrollView.contentSize = CGSizeMake(CGFloat(count) * w, 0)
        scrollView.pagingEnabled = true

        self.view .addSubview(scrollView)
        self.scrollView = scrollView
 
    
    }
    func setupTitlesView(){
    
//        UIView *titlesView = [[UIView alloc] init];
//        titlesView.frame = CGRectMake(0, XMGNavBarMaxY, self.view.xmg_width, XMGTitlesViewH);
//        titlesView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
//        [self.view addSubview:titlesView];
//        self.titlesView = titlesView;
//        
//        // 标题按钮
//        [self setupTitleButtons];
//        
//        // 下划线
//        [self setupTitleUnderline];
    
        
        let titlesView = UIView()
        titlesView.frame = CGRectMake(0, 0, self.view.frame.size.width,35)
        titlesView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(1)
        self.view.addSubview(titlesView)
        self.titlesView = titlesView
        setupTitleButtons()
        setupTitleLine()
  
    }

    func setupTitleButtons(){
//        NSArray *titles = @[@"全部", @"视频", @"声音", @"图片", @"段子"];
//        NSUInteger count = titles.count;
//        CGFloat titleButtonH = self.titlesView.xmg_height;
//        CGFloat titleButtonW = self.titlesView.xmg_width / count;
//        
//        for (NSInteger i = 0; i < count; i++) {
//            XMGTitleButton *titleButton = [[XMGTitleButton alloc] init];
//            titleButton.tag = i;
//            [titleButton setTitle:titles[i] forState:UIControlStateNormal];
//            [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//            titleButton.frame = CGRectMake(i * titleButtonW, 0, titleButtonW, titleButtonH);
//            [self.titlesView addSubview:titleButton];
//        }
        let titles = ["头条", "视频", "赛事", "靓照", "囧图","壁纸"]
        let count = titles.count
        let buttonH : CGFloat = 35.0
        let buttonW = self.titlesView.frame.size.width / CGFloat(count)
        for i  in 0..<count{
            let btn = ABTitleViewButton()
                btn.tag = i
            btn.setTitle(titles[i], forState: .Normal)
            btn.addTarget(self, action: "titleButtonClick:", forControlEvents: .TouchUpInside)
            btn.frame = CGRectMake(CGFloat(i) * buttonW, 0, buttonW, buttonH)
            self.titlesView.addSubview(btn)
        
        }
    }
    func titleButtonClick(btn : ABTitleViewButton){
        // 修改按钮的状态
        // 上一个点击的按钮 -> 暗灰色
//        self.previousClickedTitleButton.selected = NO;
//        // 新点击的按钮 -> 红色
//        titleButton.selected = YES;
//        self.previousClickedTitleButton = titleButton;
//        
//        NSInteger index = titleButton.tag;
//        [UIView animateWithDuration:0.25 animations:^{
//            // 下划线
//            self.titleUnderline.xmg_width = titleButton.titleLabel.xmg_width + XMGMargin;
//            self.titleUnderline.xmg_centerX = titleButton.xmg_centerX;
//            
//            // 滑动scrollView到对应的子控制器界面
//            CGPoint offset = self.scrollView.contentOffset;
//            offset.x = index * self.scrollView.xmg_width;
//            self.scrollView.contentOffset = offset;
//            } completion:^(BOOL finished) {
//            // 添加index位置的子控制器view到scrollView中
//            [self addChildVcViewIntoScrollView:index];
//            }];
//        
//        // 控制scrollView的scrollsToTop属性
//        for (NSInteger i = 0; i < self.childViewControllers.count; i++) {
//            UIViewController *childVc = self.childViewControllers[i];
//            
//            // 如果控制器的view没有被创建,跳过
//            if (!childVc.isViewLoaded) continue;
//            
//            // 如果控制器的view不是scrollView,就跳过
//            if (![childVc.view isKindOfClass:[UIScrollView class]]) continue;
//            
//            // 如果控制器的view是scrollView
//            UIScrollView *scrollView = (UIScrollView *)childVc.view;
//            scrollView.scrollsToTop = (i == index);
//        
        self.lastBtn.selected = false
        btn.selected = true
        self.lastBtn = btn
//        let index = btn.tag
        UIView.animateWithDuration(0.25, animations: { () -> Void in
            self.lineView.frame.size.width = btn.frame.size.width
            //            self.lineView.centerX = btn.centerX
            self.lineView.center.x = btn.center.x
            let offset : CGPoint = CGPointMake(CGFloat(btn.tag) * self.view.frame.size.width, 0)
            self.scrollView.contentOffset = offset
            }) { (finish: Bool) -> Void in
               self.addChildVcViewIntoScrollView(btn.tag)
        }
       
        
    }
    
    func addChildVcViewIntoScrollView(index : Int){
        // 取出index位置对应的子控制器
//        UIViewController *childVc = self.childViewControllers[index];
//        if (childVc.isViewLoaded) return;
//        
//        // 设置frame
//        childVc.view.frame = CGRectMake(index * self.scrollView.xmg_width, 0, self.scrollView.xmg_width, self.scrollView.xmg_height);
//        // 添加
//        [self.scrollView addSubview:childVc.view];
        let vc = self.childViewControllers[index]

        if oneBool == false {
            oneBool = true
            vc.view.frame = CGRectMake(CGFloat(index) * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)
            
            self.scrollView.addSubview(vc.view)

        }
        if vc.isViewLoaded() { return }
        vc.view.frame = CGRectMake(CGFloat(index) * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)
        
        self.scrollView.addSubview(vc.view)
    
    }
    
    func setupTitleLine(){
//        XMGTitleButton *firstTitleButton = self.titlesView.subviews.firstObject;
//        
//        UIView *titleUnderline = [[UIView alloc] init];
//        CGFloat titleUnderlineH = 2;
//        CGFloat titleUnderlineY = self.titlesView.xmg_height - titleUnderlineH;
//        titleUnderline.frame = CGRectMake(0, titleUnderlineY, 0, titleUnderlineH);
//        titleUnderline.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
//        [self.titlesView addSubview:titleUnderline];
//        self.titleUnderline = titleUnderline;
//        
//        // 新点击的按钮 -> 红色
//        firstTitleButton.selected = YES;
//        self.previousClickedTitleButton = firstTitleButton;
//        
//        // 下划线
//        [firstTitleButton.titleLabel sizeToFit];
//        self.titleUnderline.xmg_width = firstTitleButton.titleLabel.xmg_width + XMGMargin;
//        self.titleUnderline.xmg_centerX = firstTitleButton.xmg_centerX;
     

//        let change = ABTitleViewButton(firstBtn)
        let line = UIView()
        let lineH :CGFloat = 2
        let lineY :CGFloat = self.titlesView.frame.size.height - lineH
        line.frame = CGRectMake(0, lineY, 0, lineH)
        line.backgroundColor = UIColor.yellowColor()
//        ABTitleViewButton(firstBtn)
        self.titlesView.addSubview(line)
        self.lineView = line
        line.frame.size.width = (self.titlesView.subviews.first?.frame.size.width)! + 10.0
        

    
    }
    
}
extension ABHomeViewController : UIScrollViewDelegate{

   
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
       
     let i = scrollView.contentOffset.x / self.scrollView.frame.size.width
        let btn   = self.titlesView.subviews[Int(i)] as! ABTitleViewButton
        if self.lastBtn == btn{return}
          titleButtonClick(btn)
        
        

    }

}